package com.zyb.dfs.namenode.server;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 描述datanode的信息
 */
public class DataNodeInfo implements Comparable<DataNodeInfo> {

    private String ip;

    private String hostname;

    private Integer nioPort;

    private long latestHeartbeatTime;

    /**
     * 已经存储数据的大小
     */
    private long stotageDatasize;

    /**
     * 副本复制任务队列
     */
    private ConcurrentLinkedQueue<ReplicateTask> replicateTaskQueue =
            new ConcurrentLinkedQueue<ReplicateTask>();

    /**
     * 删除副本任务
     */
    private ConcurrentLinkedQueue<RemoveReplicaTask> removeReplicaTaskQueue =
            new ConcurrentLinkedQueue<RemoveReplicaTask>();

    public DataNodeInfo(String ip, String hostname, Integer nidPort) {
        this.ip = ip;
        this.hostname = hostname;
        this.latestHeartbeatTime = System.currentTimeMillis();
        this.stotageDatasize = 0;
        this.nioPort = nidPort;
    }

    public void addReplicateTask(ReplicateTask replicateTask) {
        replicateTaskQueue.offer(replicateTask);
    }

    public ReplicateTask pollReplicateTask() {
        if(!replicateTaskQueue.isEmpty()) {
            return replicateTaskQueue.poll();
        }
        return null;
    }

    public void addRemoveReplicaTask(RemoveReplicaTask removeReplicaTask) {
        removeReplicaTaskQueue.offer(removeReplicaTask);
    }

    public RemoveReplicaTask pollRemoveReplicaTask() {
        if(!removeReplicaTaskQueue.isEmpty()) {
            return removeReplicaTaskQueue.poll();
        }
        return null;
    }

    public String getId() {
        return ip + "-" + hostname;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public long getLatestHeartbeatTime() {
        return latestHeartbeatTime;
    }

    public void setLatestHeartbeatTime(long latestHeartbeatTime) {
        this.latestHeartbeatTime = latestHeartbeatTime;
    }

    public long getStotageDatasize() {
        return stotageDatasize;
    }

    public void setStotageDatasize(long stotageDatasize) {
        this.stotageDatasize = stotageDatasize;
    }

    public Integer getNioPort() {
        return nioPort;
    }

    public void setNioPort(Integer nioPort) {
        this.nioPort = nioPort;
    }

    public void addStoredDataSize(long storedDataSize) {
        this.stotageDatasize += storedDataSize;
    }

    @Override
    public int compareTo(DataNodeInfo o) {
        if (this.getStotageDatasize() - o.getStotageDatasize() > 0) {
            return 1;
        } else if (this.getStotageDatasize() - o.getStotageDatasize() < 0) {
            return -1;
        } else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return "DataNodeInfo [ip=" + ip + ", hostname=" + hostname + ", latestHeartbeatTime=" + latestHeartbeatTime
                + ", storedDataSize=" + stotageDatasize + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((hostname == null) ? 0 : hostname.hashCode());
        result = prime * result + ((ip == null) ? 0 : ip.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        DataNodeInfo other = (DataNodeInfo) obj;
        if (hostname == null) {
            if (other.hostname != null) {
                return false;
            }
        } else if (!hostname.equals(other.hostname)) {
            return false;
        }
        if (ip == null) {
            if (other.ip != null) {
                return false;
            }
        } else if (!ip.equals(other.ip)) {
            return false;
        }
        return true;
    }
}
